﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using UDataBaseWork;
using UEntity.LogEntity;
using XF.Common;

namespace DYUserCenterSync
{
    public class User_CenterSync
    {
        string strSqlConn = UCommonWork.Common.Commons.MD5DecodeString(Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["SQLConnString"]));//

        UserCenter userCenter = new UserCenter();
        public bool SYNC()
        {
            bool bResult = true;
            var sqlCon = userCenter.GetUserCenterSetting(out string strError);

            CBaseDB _db = new CBaseDB(sqlCon);
            if (!_db.ExcuteByDataAdapter(" select Guid AS dyGuid,Account,Name, PostName,Phone,Mail from UCUser_UserCenter ", out DataSet dt, out strError))
            {
                ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************获取用户信息失败, error:" + strError));
                bResult = false;
                return bResult;
            }
            if(!SqlBulkCopyInsert(strSqlConn, "DYUser_Center_Temp", dt.Tables[0]))
            {
                ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************同步用户信息至临时表失败, error:" + strError));
                bResult = false;
                return bResult;
            }
            #region  参数
            SqlParameter[] sqlParameters = new SqlParameter[] {

                                   };
            CParameters paras = new CParameters();
            paras.AddRange(sqlParameters);
            #endregion
            CBaseDB _dbmanage = new CBaseDB(strSqlConn);
            if (!_dbmanage.ExcuteByTransaction(paras, "spTranscationDYUser_Center", out strError))
            {
                bResult = false;
                ULogWork.CLogToTxtFile.RecordOperateLog(new EOperateLogEntity(" *********************同步临时表用户信息至正式表表失败, error:" + strError));
                bResult = false;
                return bResult;
            }
            return bResult;
        }

        #region 使用SqlBulkCopy将DataTable中的数据批量插入数据库中
        /// <summary>  
        /// 注意：DataTable中的列需要与数据库表中的列完全一致。 
        /// </summary>  
        #endregion
        public static bool SqlBulkCopyInsert(string strSqlConn, string strTableName, DataTable dt)
        {
            SqlTransaction tran = null;
            bool bResult = true;
            try
            {
                using (SqlConnection conn = new SqlConnection(strSqlConn))
                {
                    conn.Open();
                    using (tran = conn.BeginTransaction())
                    {
                        using (SqlBulkCopy sqlRevdBulkCopy = new SqlBulkCopy(strSqlConn))
                        {
                            sqlRevdBulkCopy.DestinationTableName = strTableName;
                            sqlRevdBulkCopy.NotifyAfter = dt.Rows.Count;
                            sqlRevdBulkCopy.WriteToServer(dt);
                            tran.Commit();
                            sqlRevdBulkCopy.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (null != tran)
                    tran.Rollback();
                bResult = false;
                throw (ex);
            }
            return bResult;
        }
    }
}